﻿using System;
using System.Linq;
using System.Text;
using System.Transactions;
using Petshop.Backend.Data;
using Petshop.Backend.Exceptions;

namespace Petshop.Backend.Logic
{
    public class AccountService
    {

        private readonly IDatabaseSession _context;

        public AccountService():this(new DatabaseSession())
        {

        }
        public AccountService(IDatabaseSession context)
        {
            _context = context;
        }
        public Account SignIn(string userId, string password)
        {
            if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(password))
                return null;
            using (var repository = new AccountRepository(_context))
            {
                return repository.FindBy( userId,  password);
            }
        }


        public void CreateNewAccount(Account account)
        {
            if (string.IsNullOrEmpty(account.UserId))
                return;
            
            using (var ts = new TransactionScope(TransactionScopeOption.Required))
            {
                using (var repository = new AccountRepository(_context))
                {
                    repository.Insert(account);
                    repository.Save();
                    ts.Complete();
                }
            }
        }

        public void UpdateAccount(Account accountInfo)
        {
            if (accountInfo == null)
                return;
            using (var ts = new TransactionScope(TransactionScopeOption.Required))
            {
                using (var repository = new AccountRepository(_context))
                {
                    var account = repository.FindBy(accountInfo.UserId);
                    if (account == null) throw new BusinessException("ACC_NOT_FOUND", "Account not found for " + accountInfo.UserId);
                    account.Addr1 = accountInfo.Addr1;
                    account.Addr2 = accountInfo.Addr2;
                    account.BannerOpt = accountInfo.BannerOpt;
                    account.City = accountInfo.City;
                    account.Country = accountInfo.Country;
                    account.Email = accountInfo.Email;
                    account.FavCategory = accountInfo.FavCategory;
                    account.FirstName = accountInfo.FirstName;
                    account.LangPref = accountInfo.LangPref;
                    account.LastName = accountInfo.LastName;
                    account.MyListOpt = accountInfo.MyListOpt;
                    account.Password = accountInfo.Password;
                    account.Phone = accountInfo.Phone;
                    account.State = accountInfo.State;
                    account.UserId = accountInfo.UserId;
                    account.Zip = accountInfo.Zip;
                    repository.Update(account);
                    repository.Save();
                    ts.Complete();
                }
            }
        }
    }
}
